<?php

class Safety_WizardController extends App_Controller_Action
{

	public function init()
    {
    	parent::init();
    }

    public function indexAction()
    {
        $this->_helper->redirector('wizard');
    }
    
    /**
     * 
     */
    public function readAction() 
    {
		$this->view->modules = $this->getModules();
    }
    
    /**
     * get every module for this application
     */
    private function getModules() 
    {
	    // recupera el controlador frontal
    	$front = $this->getFrontController();
		//recorre el for con los directorios de controladores
		$modules = array();
		foreach ($front->getControllerDirectory() as $module => $path) {
			$modules[] = $module;
		}
		return $modules;
    }
    
    /**
     * get every controller for one module
     */
    public function readControllersAction() 
    {
    	$this->_helper->ajaxContext()->addActionContext('read-controllers', 'html')->initContext();
    	$moduleName = $this->_getParam('module_name');
    	$d = dir(APPLICATION_PATH . '/modules/' . $moduleName . '/controllers') ;
    	
    	$controllerList = array();
    	while ($entry = $d->read()) {
    		if (substr($entry, 0, 1) != '.' && substr($entry, 0, 16) != 'WizardController') {
    			$controller = substr($entry, 0, strlen($entry) - 4);
    			$controllerList[$this->_camelize(substr($entry, 0, strlen($entry) - 14))] = $controller;
    		}
    	}
    	$this->view->moduleName = $moduleName;
    	$this->view->controllerList = $controllerList;
    }
    
    /**
     * get every controller for one controller
     */
	public function readActionsAction() 
    {
    	$this->_helper->ajaxContext()->addActionContext('read-actions', 'html')->initContext();
    	$moduleName = $this->_getParam('module_name');
    	$controllerName = $this->_getParam('controller_name');
    	require_once APPLICATION_PATH . "/modules/$moduleName/controllers/$controllerName.php";
    	
    	$actionList = array();
    	$controller = new ReflectionClass(($moduleName != 'website' ? ucfirst($moduleName) . '_' : '') . $controllerName);
    	foreach ($controller->getMethods(ReflectionMethod::IS_PUBLIC) as $method=>$value) {
    		if (substr($value->getName(), -6) == 'Action') {
    			$action = substr($value->getName(), 0, strlen($value->getName()) - 6);
    			$actionList[$this->_camelize($action)] = $action;
    		}
    	}
    	$this->view->actionList = $actionList;
    }
    
	/**
     * Camelize Helper for Sting Data
     *
     * @param String $str
     * @return String
     */
    private function _camelize($str, $separator = '-') {
        $str[0] = strtolower($str[0]);
        $func = create_function('$c', 'return "' . $separator . '" . strtolower($c[1]);');
        $action = preg_replace_callback('/([A-Z])/', $func, $str);
        return $action; 
    }
}

